// 导航栏滚动效果
window.addEventListener('scroll', () => {
  const navbar = document.querySelector('.navbar');
  if (window.scrollY > 400) {
    navbar.classList.add('scrolled');
  } else {
    navbar.classList.remove('scrolled');
  }
});

// 导航栏透明度随滚动渐变
window.addEventListener('scroll', () => {
  const navbar = document.querySelector('.navbar');
  const maxScroll = 900; // 渐变最大滚动距离，拉大渐变区间
  const scrollY = Math.min(window.scrollY, maxScroll);
  const alpha = scrollY / maxScroll;
  // 渐变到深红色
  navbar.style.background = `rgba(139,26,26,${alpha})`;
});

// 移动端菜单切换
const menuToggle = document.getElementById('menuToggle');
const navLinks = document.getElementById('navLinks');

if (menuToggle && navLinks) {
  menuToggle.addEventListener('click', () => {
    navLinks.classList.toggle('active');
  });
}

// 动画触发函数
function handleScrollAnimation() {
  const animatedElements = document.querySelectorAll('.animate-element, .animate-left, .animate-right, .animate-scale');
  
  animatedElements.forEach(element => {
    const elementTop = element.getBoundingClientRect().top;
    const windowHeight = window.innerHeight;
    
    // 当元素进入视口时添加动画类
    if (elementTop < windowHeight * 0.8) {
      element.classList.add('animate');
    }
  });
}

// 监听滚动事件
window.addEventListener('scroll', handleScrollAnimation);

// 页面加载时初始检查
document.addEventListener('DOMContentLoaded', () => {
  handleScrollAnimation();
  
  // 设置当前年份
  const yearElement = document.getElementById('currentYear');
  if (yearElement) {
    yearElement.textContent = new Date().getFullYear();
  }
  
  // 图片画廊悬停效果
  const galleryItems = document.querySelectorAll('.gallery-item');
  galleryItems.forEach(item => {
    item.addEventListener('mouseenter', () => {
      item.style.transform = 'translateY(-10px)';
    });
    
    item.addEventListener('mouseleave', () => {
      item.style.transform = 'translateY(0)';
    });
  });
});

// 平滑滚动
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
  anchor.addEventListener('click', function(e) {
    e.preventDefault();
    
    const targetId = this.getAttribute('href');
    if (targetId === '#') return;
    
    const targetElement = document.querySelector(targetId);
    if (targetElement) {
      // 考虑导航栏高度
      const navbarHeight = document.querySelector('.navbar').offsetHeight;
      const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - navbarHeight;
      
      window.scrollTo({
        top: targetPosition,
        behavior: 'smooth'
      });
      
      // 如果是移动端，点击后关闭菜单
      if (navLinks.classList.contains('active')) {
        navLinks.classList.remove('active');
      }
    }
  });
});

// 表单提交处理
const contactForm = document.getElementById('contactForm');
if (contactForm) {
  contactForm.addEventListener('submit', (e) => {
    e.preventDefault();
    
    // 简单的表单验证
    const name = document.getElementById('name').value;
    const email = document.getElementById('email').value;
    
    if (!name || !email) {
      alert('请填写姓名和邮箱！');
      return;
    }
    
    // 模拟表单提交
    alert('感谢您的留言！我们会尽快回复。');
    contactForm.reset();
  });
}

// 订